package com.example.interview.ui.hundred

import android.text.SpannableString
import android.text.Spanned
import android.text.style.StyleSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.interview.databinding.ItemQuestionBinding
import com.example.interview.model.Question
import android.graphics.Typeface
import java.util.regex.Pattern

class QuestionAdapter : ListAdapter<Question, QuestionAdapter.QuestionViewHolder>(QuestionDiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): QuestionViewHolder {
        val binding = ItemQuestionBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return QuestionViewHolder(binding)
    }

    override fun onBindViewHolder(holder: QuestionViewHolder, position: Int) {
        holder.bind(getItem(position))
    }

    private fun formatTextWithBold(text: String): SpannableString {
        val pattern = Pattern.compile("\\*\\*(.*?)\\*\\*")
        val matcher = pattern.matcher(text)
        val builder = StringBuilder()
        var lastEnd = 0
        val spans = mutableListOf<Pair<Int, Int>>()

        while (matcher.find()) {
            // Add text before the match
            builder.append(text.substring(lastEnd, matcher.start()))
            
            // Add the bold text without **
            val boldText = matcher.group(1)
            val start = builder.length
            builder.append(boldText)
            val end = builder.length
            
            // Record the span position
            spans.add(Pair(start, end))
            
            // Update lastEnd
            lastEnd = matcher.end()
        }
        
        // Add remaining text
        if (lastEnd < text.length) {
            builder.append(text.substring(lastEnd))
        }
        
        // Create SpannableString and apply spans
        val spannableString = SpannableString(builder.toString())
        for ((start, end) in spans) {
            spannableString.setSpan(
                StyleSpan(Typeface.BOLD),
                start,
                end,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
            )
        }
        
        return spannableString
    }

    inner class QuestionViewHolder(
        private val binding: ItemQuestionBinding
    ) : RecyclerView.ViewHolder(binding.root) {

        fun bind(question: Question) {
            binding.titleText.text = formatTextWithBold(question.title)
            binding.analysisText.text = formatTextWithBold(question.analysis)
            binding.answerText.text = formatTextWithBold(question.answer)
            
            binding.analysisText.visibility = if (question.isExpanded) View.VISIBLE else View.GONE
            binding.answerText.visibility = if (question.isExpanded) View.VISIBLE else View.GONE
            
            binding.root.setOnClickListener {
                question.isExpanded = !question.isExpanded
                binding.analysisText.visibility = if (question.isExpanded) View.VISIBLE else View.GONE
                binding.answerText.visibility = if (question.isExpanded) View.VISIBLE else View.GONE
            }
        }
    }

    private class QuestionDiffCallback : DiffUtil.ItemCallback<Question>() {
        override fun areItemsTheSame(oldItem: Question, newItem: Question): Boolean {
            return oldItem.id == newItem.id
        }

        override fun areContentsTheSame(oldItem: Question, newItem: Question): Boolean {
            return oldItem == newItem
        }
    }
} 